package com.zhongyouyun.other.thread;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.SiteListModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.model.UserSiteModel;
import com.zhongyouyun.admin.pojo.DictPojo;
import com.zhongyouyun.admin.service.SiteListService;
import com.zhongyouyun.admin.service.UserService;
import com.zhongyouyun.admin.service.UserSiteService;
import com.zhongyouyun.utils.RegexUtils;
import com.zhongyouyun.utils.SpringContextUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserSiteThread implements Runnable {

    /**
     * 线程私有属性，创建线程时创建
     */
    private String name;

    private String ip;

    public UserSiteThread(String name, String ip) {
        this.name = name;
        this.ip = ip;
    }
    @Override
    public void run() {
        UserSiteService userSiteService = (UserSiteService) SpringContextUtils.getBean(UserSiteService.class);
        SiteListService siteListService = (SiteListService) SpringContextUtils.getBean(SiteListService.class);
        UserService userService = (UserService) SpringContextUtils.getBean(UserService.class);
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_name", name);
        UserModel userModel = userService.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(userModel)) {
            QueryWrapper<UserSiteModel> userSiteModelQueryWrapper = new QueryWrapper();
            userSiteModelQueryWrapper.eq("user_id", userModel.getId());
            List<UserSiteModel> userSiteModelList = userSiteService.selectList(userSiteModelQueryWrapper);
            if (userSiteModelList.size() > 0) {
                for (UserSiteModel userSiteModel : userSiteModelList) {
                    QueryWrapper<SiteListModel> siteListModelQueryWrapper = new QueryWrapper();
                    siteListModelQueryWrapper.eq("id", userSiteModel.getSiteId());
                    SiteListModel siteListModel = siteListService.getOne(siteListModelQueryWrapper);
                    if (ObjectUtil.isNotEmpty(siteListModel)) {
                        Map map = new HashMap();
                        Object Str = ReflectUtil.invoke(userModel, RegexUtils.underlineToHump("get_"+siteListModel.getFileName()));
                        map.put("userid", String.valueOf(Str));
                        map.put("ip", ip);
                        System.out.println(siteListModel.getOutUrl());
                        String JsStr = HttpUtil.get(siteListModel.getOutUrl(), map);
                        JSONObject jsonObject = new JSONObject(JsStr);
                        if (ObjectUtil.isNotEmpty(jsonObject)) {
                            JSONObject data = jsonObject.getJSONObject("data");
                            if (ObjectUtil.isNotEmpty(data)) {
                                String state = data.getStr("state");
                                if (StrUtil.equals("1", state)) {
                                    userSiteService.removeById(userSiteModel.getId());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
